#ifdef __cplusplus
#include <cstdlib>
#else
#include <stdlib.h>
#endif
#include <SDL/SDL.h>
#include "ecran.h"
int n=2,d[100],i,j,o=0;

int ecran ( )
{
    SDL_Event event;
    SDL_Surface* screen = SDL_SetVideoMode(900,632 , 32,  SDL_HWSURFACE|SDL_DOUBLEBUF|SDL_RESIZABLE|SDL_FULLSCREEN);


    if (screen == NULL)
    {
        printf("Unable to set video mode: %s\n", SDL_GetError());
        return 1;
    }



    freopen("CON","w",stdout);
    freopen("CON","r",stdin);
    if ( SDL_Init( SDL_INIT_VIDEO ) < 0 )
    {
        printf( "Unable to init SDL: %s\n", SDL_GetError() );
        return 1;
    }

    SDL_WM_SetCaption("DOMINO", NULL);
    SDL_Surface* bmp[100];

    SDL_Surface *menu = SDL_LoadBMP("dimino.bmp");
    SDL_Surface *aide = SDL_LoadBMP("aide.bmp");
    bmp[1] = SDL_LoadBMP("piece1.bmp");
    bmp[2] = SDL_LoadBMP("piece2.bmp");
    bmp[3] = SDL_LoadBMP("piece3.bmp");
    bmp[4] = SDL_LoadBMP("piece4.bmp");
    bmp[5] = SDL_LoadBMP("piece5.bmp");
    bmp[6] = SDL_LoadBMP("piece6.bmp");
    bmp[7] = SDL_LoadBMP("piece7.bmp");
    bmp[8] = SDL_LoadBMP("piece8.bmp");
    bmp[9] = SDL_LoadBMP("piece9.bmp");
    bmp[10] = SDL_LoadBMP("piece10.bmp");
    bmp[11] = SDL_LoadBMP("piece11.bmp");
    bmp[12] = SDL_LoadBMP("piece12.bmp");
    bmp[13] = SDL_LoadBMP("piece13.bmp");
    bmp[14] = SDL_LoadBMP("piece14.bmp");
    bmp[15] = SDL_LoadBMP("piece15.bmp");
    bmp[16] = SDL_LoadBMP("piece16.bmp");
    bmp[17] = SDL_LoadBMP("piece17.bmp");
    bmp[18] = SDL_LoadBMP("piece18.bmp");
    bmp[19] = SDL_LoadBMP("piece19.bmp");
    bmp[20] = SDL_LoadBMP("piece20.bmp");
    bmp[21] = SDL_LoadBMP("piece21.bmp");
    bmp[22] = SDL_LoadBMP("piece22.bmp");
    bmp[23] = SDL_LoadBMP("piece23.bmp");
    bmp[24] = SDL_LoadBMP("piece24.bmp");
    bmp[25] = SDL_LoadBMP("piece25.bmp");
    bmp[26] = SDL_LoadBMP("piece26.bmp");
    bmp[27] = SDL_LoadBMP("piece27.bmp");
    bmp[28] = SDL_LoadBMP("piece28.bmp");
    SDL_Surface* uti1 = SDL_LoadBMP("joueur1.bmp");
    SDL_Surface* uti2 = SDL_LoadBMP("joueur2.bmp");
    SDL_Surface* ret = SDL_LoadBMP("retour.bmp");
    SDL_Rect dest;

    SDL_Rect dstrect[100];
    SDL_Rect dst;
    SDL_Rect dstut1;
    SDL_Rect dstut2;
    SDL_Rect dstret;

    dest.x = ((screen->w - menu->w) / 2);
    dest.y = ((screen->h - menu->h) / 2);
    dst.x = ((screen->w - aide->w) / 2);
    dst.y = ((screen->h - aide->h) / 2);
    dstret.x = ((screen->w - ret->w) / 2)-380;
    dstret.y = ((screen->h - ret->h) / 2)-280;
    dstut1.x = ((screen->w - uti1->w) / 2)+30;
    dstut1.y = ((screen->h - uti1->h) / 2)-250;
    dstut2.x = ((screen->w - uti2->w) / 2)+10;
    dstut2.y = ((screen->h - uti2->h) / 2)+250;

    dstrect[26].x = ((screen->w - bmp[1]->w) / 2)+40;
    dstrect[26].y = ((screen->h - bmp[1]->h) / 2);
    dstrect[28].x = ((screen->w - bmp[28]->w) / 2)+110;
    dstrect[28].y = ((screen->h - bmp[28]->h) / 2);
    dstrect[21].x = ((screen->w - bmp[21]->w) / 2)-30;
    dstrect[21].y = ((screen->h - bmp[21]->h) / 2);
    dstrect[4].x = ((screen->w - bmp[4]->w) / 2)-100;
    dstrect[4].y = ((screen->h - bmp[4]->h) / 2);
    dstrect[5].x = ((screen->w - bmp[5]->w) / 2)-200;
    dstrect[5].y = ((screen->h - bmp[5]->h) / 2)-200;
    dstrect[16].x = ((screen->w - bmp[6]->w) / 2)-130;
    dstrect[16].y = ((screen->h - bmp[6]->h) / 2)-200;
    dstrect[2].x = ((screen->w - bmp[2]->w) / 2)-60;
    dstrect[2].y = ((screen->h - bmp[2]->h) / 2)-200;
    dstrect[8].x = ((screen->w - bmp[8]->w) / 2)+10;
    dstrect[8].y = ((screen->h - bmp[8]->h) / 2)-200;
    dstrect[9].x = ((screen->w - bmp[9]->w) / 2)+80;
    dstrect[9].y = ((screen->h - bmp[9]->h) / 2)-200;
    dstrect[10].x = ((screen->w - bmp[10]->w) / 2)+150;
    dstrect[10].y = ((screen->h - bmp[10]->h) / 2)-200;
    dstrect[11].x = ((screen->w - bmp[11]->w) / 2)+220;
    dstrect[11].y = ((screen->h - bmp[11]->h) / 2)-200;
    dstrect[12].x = ((screen->w - bmp[12]->w) / 2)+220;
    dstrect[12].y = ((screen->h - bmp[12]->h) / 2)+200;
    dstrect[13].x = ((screen->w - bmp[13]->w) / 2)+150;
    dstrect[13].y = ((screen->h - bmp[13]->h) / 2)+200;
    dstrect[14].x = ((screen->w - bmp[14]->w) / 2)+80;
    dstrect[14].y = ((screen->h - bmp[14]->h) / 2)+200;
    dstrect[15].x = ((screen->w - bmp[15]->w) / 2)+10;
    dstrect[15].y = ((screen->h - bmp[15]->h) / 2)+200;
    dstrect[6].x = ((screen->w - bmp[16]->w) / 2)-60;
    dstrect[6].y = ((screen->h - bmp[16]->h) / 2)+200;
    dstrect[17].x = ((screen->w - bmp[17]->w) / 2)-130;
    dstrect[17].y = ((screen->h - bmp[17]->h) / 2)+200;
    dstrect[18].x = ((screen->w - bmp[18]->w) / 2)-200;
    dstrect[18].y = ((screen->h - bmp[18]->h) / 2)+200;
    dstrect[19].x = ((screen->w - bmp[19]->w) / 2)+400;
    dstrect[19].y = ((screen->h - bmp[19]->h) / 2)-20;
    dstrect[20].x = ((screen->w - bmp[20]->w) / 2)+400;
    dstrect[20].y = ((screen->h - bmp[20]->h) / 2)+180;
    dstrect[3].x = ((screen->w - bmp[3]->w) / 2)+400;
    dstrect[3].y = ((screen->h - bmp[3]->h) / 2)+20;
    dstrect[22].x = ((screen->w - bmp[22]->w) / 2)+400;
    dstrect[22].y = ((screen->h - bmp[22]->h) / 2)+60;
    dstrect[23].x = ((screen->w - bmp[23]->w) / 2)+400;
    dstrect[23].y = ((screen->h - bmp[23]->h) / 2)+100;
    dstrect[24].x = ((screen->w - bmp[24]->w) / 2)+400;
    dstrect[24].y = ((screen->h - bmp[24]->h) / 2)+140;
    dstrect[25].x = ((screen->w - bmp[25]->w) / 2)+400;
    dstrect[25].y = ((screen->h - bmp[25]->h) / 2)-60;
    dstrect[1].x = ((screen->w - bmp[26]->w) / 2)+400;
    dstrect[1].y = ((screen->h - bmp[26]->h) / 2)-100;
    dstrect[27].x = ((screen->w - bmp[27]->w) / 2)+400;
    dstrect[27].y = ((screen->h - bmp[27]->h) / 2)-140;
    dstrect[7].x = ((screen->w - bmp[7]->w) / 2)+400;
    dstrect[7].y = ((screen->h - bmp[7]->h) / 2)-180;



    SDL_Flip(screen);

    int done=1;
    int j1;

    while(done)
    {

        SDL_BlitSurface(menu, 0, screen, &dest);
        SDL_Flip(screen);

        while(SDL_PollEvent(&event))
        {
            switch(event.type)
            {
            case SDL_QUIT:
                done=0;
                break;

            case SDL_KEYDOWN:


                if(event.key.keysym.sym == SDLK_ESCAPE)
                    done=0;
                break;

            case SDL_MOUSEBUTTONDOWN:

                int done1=1;
                int x=event.motion.x;
                int y=event.motion.y;

                // jouer
                if(x<=770&&x>=420&&y<=153&&y>=90)
                {
                    while(done1)
                    {
                        int xm,ym;
                        SDL_GetMouseState(&xm,&ym);


                        SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 68, 107, 67));

                        SDL_BlitSurface(bmp[1], 0, screen, &dstrect[1]);
                        SDL_BlitSurface(bmp[2], 0, screen, &dstrect[2]);
                        SDL_BlitSurface(bmp[3], 0, screen, &dstrect[3]);
                        SDL_BlitSurface(bmp[4], 0, screen, &dstrect[4]);
                        SDL_BlitSurface(bmp[5], 0, screen, &dstrect[5]);
                        SDL_BlitSurface(bmp[6], 0, screen, &dstrect[6]);
                        SDL_BlitSurface(bmp[7], 0, screen, &dstrect[7]);
                        SDL_BlitSurface(bmp[8], 0, screen, &dstrect[8]);
                        SDL_BlitSurface(bmp[9], 0, screen, &dstrect[9]);
                        SDL_BlitSurface(bmp[10], 0, screen, &dstrect[10]);
                        SDL_BlitSurface(bmp[11], 0, screen, &dstrect[11]);
                        SDL_BlitSurface(bmp[12], 0, screen, &dstrect[12]);
                        SDL_BlitSurface(bmp[13], 0, screen, &dstrect[13]);
                        SDL_BlitSurface(bmp[14], 0, screen, &dstrect[14]);
                        SDL_BlitSurface(bmp[15], 0, screen, &dstrect[15]);
                        SDL_BlitSurface(bmp[16], 0, screen, &dstrect[16]);
                        SDL_BlitSurface(bmp[17], 0, screen, &dstrect[17]);
                        SDL_BlitSurface(bmp[18], 0, screen, &dstrect[18]);
                        SDL_BlitSurface(bmp[19], 0, screen, &dstrect[19]);
                        SDL_BlitSurface(bmp[20], 0, screen, &dstrect[20]);
                        SDL_BlitSurface(bmp[21], 0, screen, &dstrect[21]);
                        SDL_BlitSurface(bmp[22], 0, screen, &dstrect[22]);
                        SDL_BlitSurface(bmp[23], 0, screen, &dstrect[23]);
                        SDL_BlitSurface(bmp[24], 0, screen, &dstrect[24]);
                        SDL_BlitSurface(bmp[25], 0, screen, &dstrect[25]);
                        SDL_BlitSurface(bmp[26], 0, screen, &dstrect[26]);
                        SDL_BlitSurface(bmp[27], 0, screen, &dstrect[27]);
                        SDL_BlitSurface(bmp[28], 0, screen, &dstrect[28]);
                        SDL_BlitSurface(uti1, 0, screen, &dstut1);
                        SDL_BlitSurface(uti2, 0, screen, &dstut2);
                        SDL_BlitSurface(ret, 0, screen, &dstret);


                        SDL_Flip(screen);

                        while(SDL_PollEvent(&event))
                        {
                            switch(event.type)
                            {


                            case SDL_MOUSEBUTTONDOWN :
                                printf("%d     %d  \n",event.motion.x,event.motion.y);

                                if (SDL_BUTTON_RIGHT==event.button.button)
                                {
                                    for(j=0; j<29; j++)
                                    {
                                        d[j]=0;
                                    }
                                }



                                if(event.motion.x<=148&&event.motion.y<=51)

                                {
                                    ecran();

                                }

                                if (SDL_BUTTON_LEFT==event.button.button)
                                {
                                    for(j=0; j<29; j++)
                                    {
                                        if (dstrect[j].x<xm&&dstrect[j].x>xm-60&&dstrect[j].y<ym&&dstrect[j].y>ym-30)
                                        {
                                            o=0;
                                            for(j1=0; j1<29; j1++)
                                            {
                                                if(d[j1]==1)
                                                {
                                                    o++;
                                                }
                                            }
                                            if(o==0)
                                            {
                                                i=j;
                                                d[i]=1;
                                            }
                                        }
                                    }
                                }
                                break;


                            }

                        }
                        if(d[i]==1)
                        {

                            dstrect[i].x=xm-30;
                            dstrect[i].y=ym-15;
                        }


                    }
                }


                //  quitter
                if(x<=335&&x>=25&&y<=540&&y>=480)

                    done=0;

                // aide
                if(x>=420&&x<=770&&y>=185&&y<=250)
                {

                    while(1)
                    {
                        SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
                        SDL_BlitSurface(aide, 0, screen, &dst);
                        SDL_Flip(screen);

                        while(SDL_PollEvent(&event))
                        {
                            switch(event.type)
                            {
                            case SDL_MOUSEBUTTONDOWN:
                                int x=event.motion.x;
                                int y=event.motion.y;
                                if(x<=140&&x>=20&&y<=510&&y>=430)
                                {
                                    ecran();
                                    /* while(1)
                                         {
                                    SDL_BlitSurface(menu, 0, screen, &dest);
                                     SDL_Flip(screen);
                                     }*/

                                }
                                break;
                            }
                        }
                    }
                }

                break;
            }
        }


    }
    SDL_Quit();
    SDL_FreeSurface(bmp[100]);
    SDL_FreeSurface(screen);
    SDL_FreeSurface(menu);
    SDL_FreeSurface(aide);
    SDL_FreeSurface(uti1);
    SDL_FreeSurface(uti2);
    SDL_FreeSurface(ret);
    return 0;
}
